package com.hspedu.homework;

import java.util.Scanner;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/2/16
 * @time 14:18
 */
public class Homework01 {
    public static void main(String[] args) {
        T1 t1 = new T1();
        T2 t2 = new T2(t1);
        t1.start();
        t2.start();

    }
}

class T1 extends Thread{
    private boolean loop = true;
    @Override
    public void run() {
        //输出1-100数字
        while (loop) {
            System.out.println((int)(Math.random() * 100 + 1));
            //休眠
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("t1线程退出...");

    }

    public void setLoop(boolean loop) {
        this.loop = loop;
    }
}

class T2 extends Thread {

    private T1 t1;
    private Scanner scanner = new Scanner(System.in);

    public T2(T1 t1){
        this.t1 = t1;
    }


    public void setT1(T1 t1) {
        this.t1 = t1;
    }

    @Override
    public void run() {
        while (true){
            System.out.println("请输入指令(Q退出)：");
            char key = scanner.next().toUpperCase().charAt(0) ;
            if (key == 'Q'){
                t1.setLoop(false);
                break;
            }
        }

        System.out.println("t2退出");
    }
}
